package com.example.exammaster.service;

import com.example.exammaster.mapper.ExamMapper;
import com.example.exammaster.pojo.Exam;
import com.example.exammaster.pojo.Topic;
import com.example.exammaster.pojo.WrongExam;
import com.example.exammaster.pojo.comment;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ExamServiceImpl implements ExamService {

    @Resource
    private ExamMapper examMapper;

    @Override
    public Exam getExamById(int projectId, int examId, int projectSelectId) {
        Exam exam = new Exam();
        switch (projectId){
            case 1:exam = examMapper.getMathExam(examId,projectSelectId);;break;
            case 2:exam = examMapper.getPoliticalExam(examId,projectSelectId);break;
            case 3:exam = examMapper.getMajorExam(examId,projectSelectId);break;
        }
        return exam;
    }

    @Override
    public int addWrong(int projectId,int projectSelectId, String date,int id) {
        int result = 0;
        switch (projectId){
            case 1:if(examMapper.selectMathWrongRepeat(id,date)==null){examMapper.addMathWrong(projectSelectId,date,id);result = 1;};break;
            case 2:if(examMapper.selectPoliticalWrongRepeat(id,date)==null){examMapper.addPoliticalWrong(projectSelectId,date,id);result = 1;};break;
            case 3:if(examMapper.selectMajorWrongRepeat(id,date)==null){examMapper.addMajorWrong(projectSelectId,date,id);result = 1;};break;
        }
        return result;
    }


    @Override
    public int update(int examId,char submit) {
        return examMapper.update(examId,submit);
    }

    @Override
    public List<String> getWrongListById(int projectId, int projectSelectId) {
        List<String> list = new ArrayList<String>();
        switch (projectId){
            case 1:list = examMapper.getMathWrongList(projectSelectId);break;
            case 2:list = examMapper.getPoliticalWrongList(projectSelectId);break;
            case 3:list = examMapper.getMajorWrongList(projectSelectId);break;
        }
        return list;
    }

    @Override
    public List<String> getWrongListByIdDate(int projectId,String date, int projectSelectId) {
        List<String> list = new ArrayList<String>();
        switch (projectId){
            case 1:list = examMapper.getMathWrongListByDate(date, projectSelectId);break;
            case 2:list = examMapper.getPoliticalWrongListByDate(date, projectSelectId);break;
            case 3:list = examMapper.getMajorWrongListByDate(date, projectSelectId);break;
        }
        return list;
    }

    @Override
    public Exam getWrongQuestion(int projectId,String date,int id,int projectSelectId){
        Exam exam = new Exam();
        switch (projectId){
            case 1:exam = examMapper.getMathWrongByDate(date,id,projectSelectId);break;
            case 2:exam = examMapper.getPoliticalWrongByDate(date,id,projectSelectId);break;
            case 3:exam = examMapper.getMajorWrongByDate(date,id,projectSelectId);break;
        }
        return exam;
    }

    @Override
    public List<String> getProjectSelectList(int projectId) {
        List<String> list = new ArrayList<>();
        switch (projectId){
            case 1:list = examMapper.getMathProjectSelectList();break;
            case 2:list = examMapper.getPoliticalProjectSelectList();break;
            case 3:list = examMapper.getMajorProjectSelectList();break;
        }
        return list;
    }

    @Override
    public int getQuestionCount(int projectId, int projectSelectId) {
        int count = 0;
        switch (projectId){
            case 1:count = examMapper.getMathQuestionCount(projectSelectId);break;
            case 2:count = examMapper.getPoliticalQuestionCount(projectSelectId);break;
            case 3:count = examMapper.getMajorQuestionCount(projectSelectId);break;
        }
        return count;
    }

    @Override
    public int getWrongQuestionCount(int projectId, int projectSelectId, String date) {
        int wrongCount = 0;
        switch (projectId){
            case 1:wrongCount = examMapper.getMathWrongQuestionCount(projectSelectId,date);break;
            case 2:wrongCount = examMapper.getPoliticalWrongQuestionCount(projectSelectId,date);break;
            case 3:wrongCount = examMapper.getMajorWrongQuestionCount(projectSelectId,date);break;
        }
        return wrongCount;
    }

    @Override
    public int setWrongRate(int projectId, int projectSelectId, String date) {
        int count = 0;
        int wrongQuestionCount = 0;
        int questionCount = 0;
        double wrongRate = 0;
        double rate = 0;
        String newRate = "0";
        switch (projectId){
            case 1:
                questionCount = examMapper.getMathQuestionSolvedCount(projectSelectId,date);  //
                wrongQuestionCount = examMapper.getMathQuestionSolvedWrongCount(projectSelectId,date);
                wrongRate = (wrongQuestionCount*1.0/questionCount)*100;
//                System.out.println(questionCount);
//                System.out.println(wrongQuestionCount);
                rate = 100-wrongRate;
                newRate = String.format("%.2f",rate)+"%";
                if(examMapper.selectMathWrongRateRepeat(projectSelectId,date)==0){
                count = examMapper.setMathWrongRate(projectSelectId,newRate,date);
            }else{
                count = examMapper.updateMathWrongRate(projectSelectId,newRate,date);
            }break;
            case 2:
                questionCount = examMapper.getPoliticalQuestionSolvedCount(projectSelectId,date);
                wrongQuestionCount = examMapper.getPoliticalQuestionSolvedWrongCount(projectSelectId,date);
                wrongRate = (wrongQuestionCount*1.0/questionCount)*100;
                rate = 100-wrongRate;
                newRate = String.format("%.2f",rate)+"%";
                if(examMapper.selectPoliticalWrongRateRepeat(projectSelectId,date)==0){
                count = examMapper.setPoliticalWrongRate(projectSelectId,newRate,date);
            }else{
                count = examMapper.updatePoliticalWrongRate(projectSelectId,newRate,date);
            }break;
            case 3:
                questionCount = examMapper.getMajorQuestionSolvedCount(projectSelectId,date);
                wrongQuestionCount = examMapper.getMajorQuestionSolvedWrongCount(projectSelectId,date);
                wrongRate = (wrongQuestionCount*1.0/questionCount)*100;
                rate = 100-wrongRate;
                newRate = String.format("%.2f",rate)+"%";
                if(examMapper.selectMajorWrongRateRepeat(projectSelectId,date)==0){
                count = examMapper.setMajorWrongRate(projectSelectId,newRate,date);
            }else{
                count = examMapper.updateMajorWrongRate(projectSelectId,newRate,date);
            }break;
        }
        return count;
    }

    @Override
    public int setQuestionCount(int projectId, Integer projectSelectId, String date) {
        int result = 0;
        int count = 0;
        switch (projectId){
            case 1:
                if (examMapper.selectMathQuestionCountSolvedRepeat(projectSelectId, date)==0) {  //查找今日刷题是否为0
                    result = examMapper.insertMathQuestionSolvedCount(projectSelectId,date);
                } else {
                    count = examMapper.getMathQuestionSolvedCount(projectSelectId, date);
                    result = examMapper.updateMathQuestionSolvedCount(projectSelectId, date, ++count);
                }break;
            case 3:
                if (examMapper.selectMajorQuestionCountSolvedRepeat(projectSelectId, date)==0) {
                    result = examMapper.insertMajorQuestionSolvedCount(projectSelectId,date);
                } else{
                    count = examMapper.getMajorQuestionSolvedCount(projectSelectId,date);
                    result = examMapper.updateMajorQuestionSolvedCount(projectSelectId,date,++count);
                }break;
            case 2:
                if (examMapper.selectPoliticalQuestionCountSolvedRepeat(projectSelectId, date)==0) {
                    result = examMapper.insertPoliticalQuestionSolvedCount(projectSelectId,date);
                } else{
                    count = examMapper.getPoliticalQuestionSolvedCount(projectSelectId,date);
                    result = examMapper.updatePoliticalQuestionSolvedCount(projectSelectId,date,++count);
                }break;
        }
        return result;
    }

    @Override
    public int updateQuestionSolvedWrongCount(int projectId, int projectSelectId, String date) {
        int count = 0;
        switch (projectId){
            case 1:count= examMapper.getMathQuestionSolvedWrongCount(projectSelectId,date);examMapper.updateMathQuestionSolvedWrongCount(projectSelectId,date,++count);break;
            case 2:count= examMapper.getPoliticalQuestionSolvedWrongCount(projectSelectId,date);examMapper.updatePoliticalQuestionSolvedWrongCount(projectSelectId,date,++count);break;
            case 3:count= examMapper.getMajorQuestionSolvedWrongCount(projectSelectId,date);examMapper.updateMajorQuestionSolvedWrongCount(projectSelectId,date,++count);break;
        }
        return 1;
    }

    @Override
    public String getQuestionSolvedRate(int projectId, int projectSelectId, String date) {
        String rate = "";
        switch (projectId){
            case 1:rate = examMapper.getMathQuestionSolvedRate(projectSelectId,date);break;
            case 2:rate = examMapper.getPoliticalQuestionSolvedRate(projectSelectId,date);break;
            case 3:rate = examMapper.getMajorQuestionSolvedRate(projectSelectId,date);break;
        }
        return rate;
    }

    @Override
    public List<String> getLineData(int projectId, int projectSelectId) {
        List<String> list = new ArrayList<String>();
        switch (projectId){
            case 1: list = examMapper.getMathLineData(projectSelectId);break;
            case 2: list = examMapper.getPoliticalLineData(projectSelectId);break;
            case 3: list = examMapper.getMajorLineData(projectSelectId);break;
        }
        return list;
    }

    @Override
    public List<String> getLineDateList(int projectId, int projectSelectId) {
        List<String> list = new ArrayList<String>();
        switch (projectId){
            case 1: list = examMapper.getMathLineDateList(projectSelectId);break;
            case 2: list = examMapper.getPoliticalLineDateList(projectSelectId);break;
            case 3: list = examMapper.getMajorLineDateList(projectSelectId);break;
        }
        return list;
    }

    public String getLineName(int projectId,int projectSelectId){
        String name = "";
        switch (projectId){
            case 1: name = examMapper.getMathLineName(projectSelectId);break;
            case 2: name = examMapper.getPoliticalLineName(projectSelectId);break;
            case 3: name = examMapper.getMajorLineName(projectSelectId);break;
        }
        return name;
    }

    public int addChapterCount(int projectId,int chapter){
        int result = 0;
        int count = 0;
        switch (projectId){
            case 1:count = examMapper.selectMathChapterCount(chapter);examMapper.updateMathChapterCount(chapter,++count);result = 1;break;
            case 2:count = examMapper.selectPoliticalChapterCount(chapter);examMapper.updatePoliticalChapterCount(chapter,++count);result = 1;break;
            case 3:count = examMapper.selectMajorChapterCount(chapter);examMapper.updateMajorChapterCount(chapter,++count);result = 1;break;
        }
        calChapterRate(projectId,chapter);
        return result;
    }

    public int addChapterWrongCount(int projectId,int chapter){
        int result = 0;
        int wrongCount = 0;
        switch (projectId) {
            case 1:
                wrongCount = examMapper.selectMathChapterWrongCount(chapter);
                examMapper.updateMathChapterWrongCount(chapter, ++wrongCount);
                result = 1;
                break;
            case 2:
                wrongCount = examMapper.selectPoliticalChapterWrongCount(chapter);
                examMapper.updatePoliticalChapterWrongCount(chapter, ++wrongCount);
                result = 1;
                break;
            case 3:
                wrongCount = examMapper.selectMajorChapterWrongCount(chapter);
                examMapper.updateMajorChapterWrongCount(chapter, ++wrongCount);
                result = 1;
                break;
        }
        return result;
    }

    public void calChapterRate(int projectId,int chapter){
        int count = 0;
        int wrongCount = 0;
        double rate = 0;
        switch (projectId) {
            case 1:
                count = examMapper.selectMathChapterCount(chapter);
                wrongCount = examMapper.selectMathChapterWrongCount(chapter);
                rate = 100-(wrongCount*1.0/count)*100;
                examMapper.calMathChapterRate(chapter,rate);
                break;
            case 2:
                count = examMapper.selectPoliticalChapterCount( chapter);
                wrongCount = examMapper.selectPoliticalChapterWrongCount(chapter);
                rate = 100-(wrongCount*1.0/count)*100;
                examMapper.calPoliticalChapterRate(chapter,rate);
                break;
            case 3:
                count = examMapper.selectMajorChapterCount(chapter);
                wrongCount = examMapper.selectMajorChapterWrongCount(chapter);
                rate = 100-(wrongCount*1.0/count)*100;
                examMapper.calMajorChapterRate(chapter,rate);
                break;
        }
    }

    @Override
    public double[] getChapterRate(int projectId, int projectSelectId) {
        double[] res = new double[50];
        switch (projectId){
            case 1:res = examMapper.getMathChapterRate(projectSelectId);break;
            case 2:res = examMapper.getPoliticalChapterRate(projectSelectId);break;
            case 3:res = examMapper.getMajorChapterRate(projectSelectId);break;
        }
        return res;
    }

    @Override
    public Exam getChapterQuestion(int projectId, int projectSelectId, int chapterId,int questionId) {
        Exam exam = new Exam();
        switch (projectId){
            case 1:exam = examMapper.getMathChapterQuestion(projectSelectId,chapterId,questionId);break;
            case 2:exam = examMapper.getPoliticalChapterQuestion(projectSelectId,chapterId,questionId);break;
            case 3:exam = examMapper.getMajorChapterQuestion(projectSelectId,chapterId,questionId);break;
        }
        return exam;
    }

    @Override
    public int getChapterCount(int projectId, int projectSelectId, int chapterId) {
        int count = 0;
        switch (projectId){
            case 1:count = examMapper.getMathChapterCount(projectSelectId,chapterId);break;
            case 2:count = examMapper.getPoliticalChapterCount(projectSelectId,chapterId);break;
            case 3:count = examMapper.getMajorChapterCount(projectSelectId,chapterId);break;
        }
        return count;
    }

    @Override
    public List<String> getShortList(int projectId, int projectSelectId) {
        List<String> list = new ArrayList<>();
        switch (projectId){
            case 1:list = examMapper.selectMathShortList(projectSelectId);break;
            case 2:list = examMapper.selectPoliticalShortList(projectSelectId);break;
            case 3:list = examMapper.selectMajorShortList(projectSelectId);break;
        }
        return list;
    }

    @Override
    public List<String> getYearList(int projectId, int projectSelectId) {
        List<String> list = new ArrayList<>();
        switch (projectId){
            case 1:list = examMapper.getMathYearList(projectSelectId);break;
            case 2:list = examMapper.getPoliticalYearList(projectSelectId);break;
            case 3:list = examMapper.getMajorYearList(projectSelectId);break;
        }
        return list;
    }

    @Override
    public int[] getExamIdList(int projectId, int projectSelectId) {
        int[] arr = new int[100];
        switch (projectId){
            case 1:arr = examMapper.getMathExamIdList(projectSelectId);break;
            case 2:arr = examMapper.getPoliticalExamIdList(projectSelectId);break;
            case 3:arr = examMapper.getMajorExamIdList(projectSelectId);break;
        }
        return arr;
    }

    @Override
    public int getChapterIdList(int projectId,int examId) {
        int chapterId = 0;
        switch (projectId){
            case 1:chapterId = examMapper.getMathChapterId(examId);break;
            case 2:chapterId = examMapper.getPoliticalChapterId(examId);break;
            case 3:chapterId = examMapper.getMajorChapterId(examId);break;
        }
        return chapterId;
    }

    @Override
    public int getCountByChapterId(int projectId, int projectSelectId, int chapterId) {
        int count = 0;
        switch (projectId){
            case 1:count = examMapper.getMathCountByChapterId(projectSelectId,chapterId);break;
            case 2:count = examMapper.getPoliticalCountByChapterId(projectSelectId,chapterId);break;
            case 3:count = examMapper.getMajorCountByChapterId(projectSelectId,chapterId);break;
        }
        return count;
    }

    @Override
    public List<Topic> getTopicList() {
        List<Topic> list = new ArrayList<>();
        list = examMapper.getTopicList();
        return list;
    }

    @Override
    public void addTopic(String img, String title, String date, String submitImg) {
        examMapper.addTopic(img,title,date,submitImg);
    }


    @Override
    public List<String> getChapterList(int projectId, int projectSelectId) {
        List<String> list = new ArrayList<>();
        switch (projectId){
            case 1:list = examMapper.selectMathChapterList(projectSelectId);break;
            case 2:list = examMapper.selectPoliticalChapterList(projectSelectId);break;
            case 3:list = examMapper.selectMajorChapterList(projectSelectId);break;
        }
        return list;
    }

    public int getStudyDay(int projectId,int projectSelectId){
        int count = 0;
        switch (projectId){
            case 1:count = examMapper.getMathStudyDay(projectSelectId);break;
            case 2:count = examMapper.getPoliticalStudyDay(projectSelectId);break;
            case 3:count = examMapper.getMajorStudyDay(projectSelectId);break;
        }
        return count;
    }

    public Topic getTopic(int id){
        return examMapper.getTopic(id);
    }

    @Override
    public void addComment(int topicId, String img, String comment, String date) {
        examMapper.addComment(topicId,img,comment,date);
    }

    @Override
    public List<comment> getComment(int topicId) {
        return examMapper.getComment(topicId);
    }

    @Override
    public void addMathQuestion(String title,String answerA,String answerB,String answerC,String answerD,String realAnswer,int projectSelectId,int chapterId,String examYear,String isImage) {
        examMapper.addMathQuestion(title,answerA,answerB,answerC,answerD,realAnswer,projectSelectId,chapterId,examYear,isImage);
    }

    @Override
    public List<Exam> getMathQuestion() {
        return examMapper.getMathQuestion();
    }

    @Override
    public List<Exam> selectMathQuestion(int projectSelectId, int chapterId) {
        return examMapper.selectMathQuestion(projectSelectId,chapterId);
    }

    @Override
    public Exam getMathQuestionById(int examId) {
        return examMapper.getMathQuestionById(examId);
    }

    @Override
    public void updateMathQuestionById(String title, String answerA, String answerB, String answerC, String answerD, String realAnswer, int projectSelectId, String examYear, String isImage, int examId) {
        examMapper.updateMathQuestionById(title,answerA,answerB,answerC,answerD,realAnswer,projectSelectId,examYear,isImage,examId);
    }

    @Override
    public List<Exam> getPoliticalQuestion() {
        return examMapper.getPoliticalQuestion();
    }

    @Override
    public List<Exam> getMajorQuestion() {
        return examMapper.getMajorQuestion();
    }

    public List<WrongExam> getMathWrongQuestion(){
        return examMapper.getMathWrongQuestion();
    }

    @Override
    public List<WrongExam> getPoliticalWrongQuestion() {
        return examMapper.getPoliticalWrongQuestion();
    }

    @Override
    public List<WrongExam> getMajorWrongQuestion() {
        return examMapper.getMajorWrongQuestion();
    }

    @Override
    public List<comment> getCommentList() {
        return examMapper.getCommentList();
    }


}
